Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow complex values in variables parameter of terraform module #4281

Conversation

wmudge
Copy link
Contributor

@wmudge wmudge commented Feb 23, 2022

Signed-off-by: Webster Mudge [email protected]

SUMMARY

Allow complex, i.e. lists and dictionaries, values in the variables parameter of the terraform module. Without this minor patch, complex values will fail due to formatting issues, i.e. Single quotes are not valid. Use double quotes (\") to enclose strings.

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

community.general.terraform

@wmudge wmudge changed the title Allow complex values in variables parameter Allow complex values in variables parameter of terraform module Feb 23, 2022
@ansibullbot
Copy link
Collaborator

@ansibullbot ansibullbot added bug This issue/PR relates to a bug cloud module module new_contributor Help guide this first time contributor plugins plugin (any type) small_patch Hopefully easy to review labels Feb 23, 2022
Copy link
Collaborator

@felixfontein felixfontein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution! Can you please add a changelog fragment? Thanks.

@@ -443,7 +443,7 @@ def main():
for k, v in variables.items():
variables_args.extend([
'-var',
'{0}={1}'.format(k, v)
'{0}={1}'.format(k, json.dumps(v))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does terraform interpret arguments of -var? I'm wondering whether this is a breaking change or not.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please comment on this one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe so, as I haven't hit any issues with this patch in my use cases. That said, I just reread the TF docs on the topic (see Input Variables on the Command Line and Assigning Values to Root Module Variables), and the references all seem to point to TF "native" expressions. However, JSON Configuration Syntax states that "Everything that can be expressed in native syntax can also be expressed in JSON syntax, but some constructs are more complex to represent in JSON due to limitations of the JSON grammar."

I'll run some more tests to see if I can break this, but I would think that if you have really complex variables, you are best served by creating your own variables file and passing that as a reference. The intention of this patch is to allow "simple" complex variables, like list(string), to be declared in the variables parameter.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your explanations and links! Sounds convincing to me. (I'm not using terraform so I can't test myself.)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has turned out to be a breaking change. See #4367.

@felixfontein felixfontein added backport-4 check-before-release PR will be looked at again shortly before release and merged if possible. labels Feb 23, 2022
Signed-off-by: Webster Mudge <[email protected]>
@ansibullbot ansibullbot removed the small_patch Hopefully easy to review label Feb 23, 2022
@ansibullbot ansibullbot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR labels Feb 24, 2022
@felixfontein
Copy link
Collaborator

The change looks reasonable to me. If nobody complains, I'll merge in ~a week.

@felixfontein felixfontein removed the check-before-release PR will be looked at again shortly before release and merged if possible. label Mar 10, 2022
@felixfontein felixfontein merged commit 4cc7f41 into ansible-collections:main Mar 10, 2022
@patchback
Copy link

patchback bot commented Mar 10, 2022

Backport to stable-3: 💚 backport PR created

✅ Backport PR branch: patchback/backports/stable-3/4cc7f413953606cda79596c7682eac0319936199/pr-4281

Backported as #4340

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

@felixfontein
Copy link
Collaborator

@wmudge thanks a lot for your contribution!

patchback bot pushed a commit that referenced this pull request Mar 10, 2022
* Allow complex values in variables parameter

Signed-off-by: Webster Mudge <[email protected]>

* Add changelog fragment

Signed-off-by: Webster Mudge <[email protected]>

* Update changelogs fragments formatting

Co-authored-by: Felix Fontein <[email protected]>

Co-authored-by: Felix Fontein <[email protected]>
(cherry picked from commit 4cc7f41)
@patchback
Copy link

patchback bot commented Mar 10, 2022

Backport to stable-4: 💚 backport PR created

✅ Backport PR branch: patchback/backports/stable-4/4cc7f413953606cda79596c7682eac0319936199/pr-4281

Backported as #4341

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

patchback bot pushed a commit that referenced this pull request Mar 10, 2022
* Allow complex values in variables parameter

Signed-off-by: Webster Mudge <[email protected]>

* Add changelog fragment

Signed-off-by: Webster Mudge <[email protected]>

* Update changelogs fragments formatting

Co-authored-by: Felix Fontein <[email protected]>

Co-authored-by: Felix Fontein <[email protected]>
(cherry picked from commit 4cc7f41)
felixfontein pushed a commit that referenced this pull request Mar 11, 2022
… (#4340)

* Allow complex values in variables parameter

Signed-off-by: Webster Mudge <[email protected]>

* Add changelog fragment

Signed-off-by: Webster Mudge <[email protected]>

* Update changelogs fragments formatting

Co-authored-by: Felix Fontein <[email protected]>

Co-authored-by: Felix Fontein <[email protected]>
(cherry picked from commit 4cc7f41)

Co-authored-by: Webster Mudge <[email protected]>
felixfontein pushed a commit that referenced this pull request Mar 11, 2022
… (#4341)

* Allow complex values in variables parameter

Signed-off-by: Webster Mudge <[email protected]>

* Add changelog fragment

Signed-off-by: Webster Mudge <[email protected]>

* Update changelogs fragments formatting

Co-authored-by: Felix Fontein <[email protected]>

Co-authored-by: Felix Fontein <[email protected]>
(cherry picked from commit 4cc7f41)

Co-authored-by: Webster Mudge <[email protected]>
@earchibald-lv
Copy link

earchibald-lv commented Mar 15, 2022

This has completely broken my existing playbooks. See #4367.

2 hours of "what could possibly have changed, what do you mean "us-east-1" is an invalid region?" later...

@earchibald-lv
Copy link

You could have simply told people to pass complex values through the to_json filter on their own.

This "fix" forces everything through json.dumps() and completely incorrectly escapes things that should NOT BE escaped.

@earchibald-lv
Copy link

Specifically, this breaks PLAIN STRINGS.

"string" becomes json-escaped: "\"string\"", which fails at the terraform layer.

Complex data that has already gone through a to_json filter is DOUBLE JSON-escaped, which fails at the terraform layer.

@mkjpryor
Copy link

mkjpryor commented Mar 15, 2022

@earchibald-lv is correct - this change is not just not backwards compatible, but is also actually totally broken in the way that it now handles string values.

I have some code that uses the terraform module that has variables of type string and variables of type list(string). For the list(string) variables, I am using similar statements to this, which worked as of 4.5.0:

cluster_ssh_public_keys: "{{ [cluster_deploy_ssh_public_key, cluster_user_ssh_public_key] | to_json }}"

However at 4.6.0 this produces a validation error. I can fix this for 4.6.0 by switching to:

cluster_ssh_public_keys:
  - "{{ cluster_deploy_ssh_public_key }}"
  - "{{ cluster_user_ssh_public_key }}"

which is fine - in fact I probably prefer it. But I then get bitten by the following error:

Error: Error trying to get network information from the Network API: Could not find any matching network for name "portal-internal"

Now this network definitely exists, and the code was working up until yesterday so I was mystified as to what was going on, because the error message seems sensible. It took me ages to figure out that the reason it was failing is because the quotes are being treated as part of the network name.

For now, I will be sticking at 4.5.0, unfortunately.

@earchibald-lv
Copy link

I believe the backwards-compatible fix may be just "don't encode strings as JSON, other types are fine" but there should probably be a more comprehensive look at this before a new release.

I'd very much like a 4.6.1 with this change removed, and to see this circled back to in a later release, with time to be better-tested. I agree with @mkjpryor that being able to get rid of all the filter pipes to_json would be lovely!

@mkjpryor
Copy link

@earchibald-lv I think it is easier than that - the fix can be "only encode dict and list as JSON" right?

@mkjpryor
Copy link

There may also be others like bool and None I guess.

@mkjpryor
Copy link

I actually don't mind the | to_json pipes that much, as it means I am in control. I could manually build those strings if I had to in order to do something complex.

@earchibald-lv
Copy link

earchibald-lv commented Mar 15, 2022

The "oh, jeez, what about that case" is the classic path to an unexpected break like this :)

I agree re: the filters and control, which skipping strings would allow*. The most important part is that it is compatible and predicable and tested.

  • Then begins the best practice war and that is an entirely different saga

@mkjpryor
Copy link

@earchibald-lv Totally agree.

IMHO, this PR should be reverted and a 4.6.1 release cut. In order to re-propose the change, we need a test suite that covers as many different types of variable as possible to ensure this doesn’t happen again. But I’m actually happy with how it is at 4.5.0 TBH.

@felixfontein
Copy link
Collaborator

I agree this should better be reverted for now.

PR to revert this change from main and stable-3 branches: #4368. Will manually backport to stable-4 and add changelog fragment (since there has been a release from stable-4) once that's merged.

@felixfontein
Copy link
Collaborator

we need a test suite that covers as many different types of variable as possible to ensure this doesn’t happen again.

Some useful tests would be a good idea indeed. Right now there is one single unit test in tests/unit/plugins/modules/cloud/misc/test_terraform.py which checks that the module fails if called without any arguments. This isn't much more helpful than having no tests at all.

It would also be helpful if more folks could look at proposed changes and comment on them.

felixfontein added a commit that referenced this pull request Mar 16, 2022
patchback bot pushed a commit that referenced this pull request Mar 16, 2022
felixfontein added a commit to felixfontein/community.general that referenced this pull request Mar 16, 2022
felixfontein added a commit that referenced this pull request Mar 16, 2022
…le (#4281)" (#4368) (#4369)

This reverts commit 4cc7f41.

(cherry picked from commit 9618fb9)

Co-authored-by: Felix Fontein <[email protected]>
felixfontein added a commit that referenced this pull request Mar 16, 2022
…raform module (#4281)" (#4370)

* Revert "Allow complex values in variables parameter of terraform module (#4281)" (#4368)

This reverts commit 4cc7f41.

(cherry picked from commit 9618fb9)

* Add changelog fragment.
@wmudge wmudge deleted the bugfix/terraform-complex-variables branch March 16, 2022 19:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue/PR relates to a bug cloud module module new_contributor Help guide this first time contributor plugins plugin (any type)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants